package org.kl.auditsupervision.web.fnd;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.kl.auditsupervision.entity.fnd.AuditProDtlNop;
import org.kl.auditsupervision.entity.fnd.AuditProNop;
import org.kl.auditsupervision.entity.fnd.Employee;
import org.kl.auditsupervision.service.fnd.AuditProDtlNopService;
import org.kl.auditsupervision.service.fnd.AuditProNopService;
import org.kl.bf.entity.basic.Role;
import org.kl.bf.entity.easyui.EasyuiUtils;
import org.kl.bf.service.ServiceException;
import org.kl.bf.service.basic.AttachmentService;
import org.kl.bf.service.basic.RoleService;
import org.kl.bf.utils.KLServlets;
import org.kl.bf.web.base.BasicController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.MediaTypes;

/**
 * 
 * 审计表
 * @author LJ
 *
 */
@Controller
@RequestMapping(value = "/fnd/auditProNop")
public class AuditProNopController extends BasicController {

	@Autowired
	private AuditProNopService auditProNopService;
	@Autowired
	private AuditProDtlNopService auditProDtlNopService;
	@Autowired
	private AttachmentService attachmentService;
	@Autowired
	private RoleService roleService;
	
	private String employeeId;//用户Id

	@RequiresPermissions(value = "auditProNop:list")
	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model) {
		//model.addAttribute("type", 0);
		return "fnd/auditProNopList";
	}
	
	@RequiresPermissions(value = "auditProNop:list")
	@RequestMapping(value = "0",method = RequestMethod.GET)
	public String list0(Model model) {
		model.addAttribute("type", 0);
		return "fnd/auditProNopList";
	}
	
	@RequiresPermissions(value = "auditProNop:list")
	@RequestMapping(value = "1",method = RequestMethod.GET)
	public String list2(Model model) {
		model.addAttribute("type", 1);
		return "fnd/auditProNopList";
	}
	
	@RequiresPermissions(value = "auditProNop:list")
	@RequestMapping(value = "2",method = RequestMethod.GET)
	public String list3(Model model) {
		model.addAttribute("type", 2);
		return "fnd/auditProNopList";
	}
	
	@RequiresPermissions(value = "auditProNop:list")
	@RequestMapping(value = "3",method = RequestMethod.GET)
	public String list4(Model model) {
		model.addAttribute("type", 3);
		return "fnd/auditProNopList";
	}

	@RequiresPermissions(value = "auditProNop:list")
	@RequestMapping(value = "auditShowList", method = RequestMethod.GET,produces = MediaTypes.JAVASCRIPT_UTF_8)
	public String AuditShowList() {
		return "fnd/auditShowList";
	}

	@RequestMapping(value = "detail")
	public String detail(@RequestParam(value = "id", defaultValue = "") String id,
			@RequestParam(value = "businessKey", defaultValue = "") String businessKey, Model model,
			HttpServletRequest request) {
		AuditProNop auditProNop;
		if(StringUtils.isBlank(id)){
			auditProNop=auditProNopService.getOne(businessKey);
		}
		else{
			auditProNop=auditProNopService.getOne(id);
		}
		model.addAttribute("type", auditProNop.getType());
		model.addAttribute("auditProNop", auditProNop);
		return "fnd/auditProDtlNopList";
	}

	// 获取数据
	@RequiresPermissions(value = "auditProNop:list")
	@RequestMapping(value = "dataGrid", produces = { MediaTypes.TEXT_HTML_UTF_8, MediaTypes.JSON_UTF_8 })
	@ResponseBody
	public String dataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "rows", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sort", defaultValue = "createTime") String sortType,
			@RequestParam(value = "order", defaultValue = "desc") String orderType, Model model, ServletRequest request) {
		try {
			Map<String, Object> searchParams = KLServlets.getParametersStartingWith(request, SEARCH_PREFIX);
			createDeptFilter(searchParams);
			Page<AuditProNop> auditProNop = auditProNopService.getPage(searchParams, pageNumber, pageSize, sortType,
					orderType);
			return EasyuiUtils.buildEasyuiGridJsonString(auditProNop.getContent(), (int) auditProNop.getTotalElements());
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("删除失败:" + e.getMessage(), false, null);
		}
	}
	

	@RequiresPermissions(value = "auditProNop:delete")
	@RequestMapping(value = "multiDelete", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String multiDelete(@RequestParam(value = "ids", defaultValue = "") String ids) {
		String msg = "";
		try {
			String[] idArray = StringUtils.split(ids, ",");
			for(String id:idArray){
				if(StringUtils.isNotBlank(id)){
 				   List<AuditProDtlNop> list=auditProDtlNopService.findByAuditProId(id);//删除明细
 				   if(list.size()>0){
 					  throw new ServiceException("该项目存在明细,请核对!");
 				   }
 				       
				}
			}
			msg = auditProNopService.multiDelete(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("删除失败:" + e.getMessage(), false, null);
		}
		return ajaxMsg("删除成功:" + msg, true, null);
	}

	// 添加与更新
	@RequiresPermissions(value = "auditProNop:update")
	@RequestMapping(value = "update", method = RequestMethod.POST, produces = { MediaTypes.TEXT_HTML_UTF_8,
			MediaTypes.JSON_UTF_8 })
	@ResponseBody
	public String update(@Valid @ModelAttribute("auditPro") AuditProNop auditProNop, ServletRequest request) {
		try {
			entityStatusValidate(auditProNop);
			if(StringUtils.isBlank(auditProNop.getId())){
				List<AuditProNop> auditProNopList=auditProNopService.findByAuditName(auditProNop.getAuditName());
				if(!auditProNopList.isEmpty()){
					return ajaxMsg("保存失败,名称不能重复!", false, null);
				}
			}
			auditProNopService.save(auditProNop);
			return ajaxMsg("保存成功!", true, null);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("保存失败!", false, null);
		}
	}
	@RequiresPermissions("auditProNop:export")
	@RequestMapping(value = "excelExport", produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String excelExport(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "sort", defaultValue = "id") String sortType,
			@RequestParam(value = "order", defaultValue = "desc") String orderType, Model model, ServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> searchParams = KLServlets.getParametersStartingWith(request, SEARCH_PREFIX);
		List<AuditProNop> auditProNop =auditProNopService.searchAll(searchParams, sortType, orderType);
		super.export(AuditProNop.class, response, auditProNop, "审计表");
		return null;
	}
	 
	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2
	 * Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
	 */
	@ModelAttribute
	public void init(@RequestParam(value = "id", defaultValue = "-1") String id, Model model) {
		if (!id.equals("-1")) {
			model.addAttribute("auditProNop", auditProNopService.getOne(id));
		}
	}
}
